package Task;

import Service.StockService;
import Utils.GetStockInfo;
import Utils.Stock;
import Utils.StockInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

@Component
@Lazy(false)
public class StockUpdateTask
{
    @Autowired
    private StockService stockService;
    @Scheduled(fixedRate = 600000)
    public void UpdateStock()
    {
        Map<String,Integer> map = new LinkedHashMap<>();
        ArrayList<Stock> Stocks_Exit = null;
        Stocks_Exit = stockService.getAllStock();
        ArrayList<Stock> Stock_Now = null;
        Stock_Now = GetStockInfo.getStock();
        for(Stock s: Stocks_Exit)
        {
            map.put(s.getStockCode(),0);
        }
        ArrayList<Stock> Stock_new = new ArrayList<Stock>();
        for(Stock s: Stock_Now)
        {
            if(map.containsKey(s.getStockCode()))
                continue;
            Stock_new.add(s);
        }
        stockService.UpdateStock(Stock_new);
    }
    @Scheduled(fixedRate = 600000)
    public void updateStock()
    {
        stockService.TruncateStockInfo();
        stockService.UpdateStockInfo();
    }
}
